package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.IAttendGoOutRuleDAO;
import org.jsola.hr.dto.AttendGoOutRuleAddDTO;
import org.jsola.hr.dto.AttendGoOutRuleUpdateDTO;
import org.jsola.hr.entity.AttendGoOutRuleDO;
import org.jsola.hr.query.AttendGoOutRuleQuery;
import org.jsola.hr.service.IAttendGoOutRuleService;
import org.jsola.hr.service.ICompanyService;
import org.jsola.hr.service.IDepInfoService;
import org.jsola.hr.vo.AttendGoOutRuleListVO;
import org.jsola.hr.vo.AttendGoOutRuleVO;
import org.jsola.hr.vo.CompanyVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhr
 */
@Service("hrAttendGoOutRuleServiceImpl")
public class AttendGoOutRuleServiceImpl implements IAttendGoOutRuleService {

    @Autowired
    private IAttendGoOutRuleDAO attendGoOutRuleDAO;
    @Autowired
    private IDepInfoService depInfoService;
    @Autowired
    private ICompanyService companyService;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public AttendGoOutRuleVO save(AttendGoOutRuleAddDTO attendGoOutRuleAddDTO, TokenUser tokenUser) {
        // 转DO
        AttendGoOutRuleDO attendGoOutRuleDO = attendGoOutRuleAddDTO.to(AttendGoOutRuleDO.class);

        // 查询公司信息
        CompanyVO companyInfo = companyService.selectBaseCompanyById(attendGoOutRuleAddDTO.getCompanyId(), tokenUser.getSiteId());

        attendGoOutRuleDO.setCompanyId(companyInfo.getId());
        attendGoOutRuleDO.setCompanyName(companyInfo.getName());
        // 保存
        attendGoOutRuleDO = save(attendGoOutRuleDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return attendGoOutRuleDO.to(AttendGoOutRuleVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(AttendGoOutRuleUpdateDTO attendGoOutRuleUpdateDTO, TokenUser tokenUser) {
        //转DO
        AttendGoOutRuleDO attendGoOutRuleDO = attendGoOutRuleUpdateDTO.to(AttendGoOutRuleDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(attendGoOutRuleDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... attendGoOutRuleIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) attendGoOutRuleIds);
    }


    @Override
    public AttendGoOutRuleVO selectById(String attendGoOutRuleId, String siteId) {
        AttendGoOutRuleDO attendGoOutRuleDO = selectDOById(attendGoOutRuleId, siteId);
        if (attendGoOutRuleDO == null) {
            return null;
        }
        return attendGoOutRuleDO.to(AttendGoOutRuleVO.class);
    }

    @Override
    public List<AttendGoOutRuleListVO> select(AttendGoOutRuleQuery attendGoOutRuleQuery, String siteId) {
        List<AttendGoOutRuleDO> attendGoOutRuleDOList = selectDO(attendGoOutRuleQuery, siteId);
        if(CollectionUtils.isEmpty(attendGoOutRuleDOList)) {
            return attendGoOutRuleDOList == null ? null : new ArrayList<>();
        }
        return attendGoOutRuleDOList.stream()
                .map(attendGoOutRuleDO -> attendGoOutRuleDO.to(AttendGoOutRuleListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(AttendGoOutRuleQuery attendGoOutRuleQuery, String siteId) {
        Example example = buildExample(attendGoOutRuleQuery,siteId);
        return attendGoOutRuleDAO.selectCountByExample(example);
    }

    @Override
    public Page<AttendGoOutRuleListVO> selectPage(AttendGoOutRuleQuery attendGoOutRuleQuery, String siteId) {
        Example example = buildExample(attendGoOutRuleQuery,siteId);
        Page<AttendGoOutRuleDO> page = attendGoOutRuleDAO.selectPageByExample(example,
                attendGoOutRuleQuery.getPageNo(),
                attendGoOutRuleQuery.getPageSize());

        return page.to(AttendGoOutRuleListVO.class);
    }

    @Override
    public AttendGoOutRuleDO selectDOById(String attendGoOutRuleId, String siteId) {
        return listById(attendGoOutRuleId, siteId);
    }

    @Override
    public List<AttendGoOutRuleDO> selectDO(AttendGoOutRuleQuery attendGoOutRuleQuery, String siteId) {
        Example example = buildExample(attendGoOutRuleQuery,siteId);
        return attendGoOutRuleDAO.selectByExample(example);
    }

    @Override
    public AttendGoOutRuleVO selectOneByCompanyId(String companyId, String siteId) {

        AttendGoOutRuleQuery attendGoOutRuleQuery = new AttendGoOutRuleQuery();
        attendGoOutRuleQuery.setCompanyId(companyId);
        Example example = buildExample(attendGoOutRuleQuery,siteId);

        List<AttendGoOutRuleDO> dataList = attendGoOutRuleDAO.selectByExample(example);
        if (dataList.size() <= 0) {
            return null;
        }

        return dataList.get(0).to(AttendGoOutRuleVO.class);
    }


    /**
     * 根据查询参数，构建example

     * @param attendGoOutRuleQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(AttendGoOutRuleQuery attendGoOutRuleQuery, String siteId) {
        Example example = new Example(AttendGoOutRuleDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        if (attendGoOutRuleQuery.getCompanyId() != null) {
            example.and().andLike("companyId", "%" + attendGoOutRuleQuery.getCompanyId() + "%");
        }
        if (attendGoOutRuleQuery.getCompanyName() != null) {
            example.and().andLike("companyName", "%" + attendGoOutRuleQuery.getCompanyName() + "%");
        }
        if (attendGoOutRuleQuery.getCollectStartTime() != null) {
            example.and().andEqualTo("collectStartTime", attendGoOutRuleQuery.getCollectStartTime());
        }
        if (attendGoOutRuleQuery.getCollectEndTime() != null) {
            example.and().andEqualTo("collectEndTime", attendGoOutRuleQuery.getCollectEndTime());
        }
        if (attendGoOutRuleQuery.getIsTakePhoto() != null) {
            example.and().andEqualTo("isTakePhoto", attendGoOutRuleQuery.getIsTakePhoto());
        }
        if (attendGoOutRuleQuery.getIsAllowLocationPhoto() != null) {
            example.and().andEqualTo("isAllowLocationPhoto", attendGoOutRuleQuery.getIsAllowLocationPhoto());
        }
        if (attendGoOutRuleQuery.getGmtCreateStart() != null &&
                attendGoOutRuleQuery.getGmtCreateEnd() != null) {
            example.and().andBetween("gmtCreate", attendGoOutRuleQuery.getGmtCreateStart(),
                    attendGoOutRuleQuery.getGmtCreateEnd());
        }
        if (attendGoOutRuleQuery.getCreateUserId() != null) {
            example.and().andLike("createUserId", "%" + attendGoOutRuleQuery.getCreateUserId() + "%");
        }
        if (attendGoOutRuleQuery.getGmtModifiedStart() != null &&
                attendGoOutRuleQuery.getGmtModifiedEnd() != null) {
            example.and().andBetween("gmtModified", attendGoOutRuleQuery.getGmtModifiedStart(),
                    attendGoOutRuleQuery.getGmtModifiedEnd());
        }
        if (attendGoOutRuleQuery.getModifiedUserId() != null) {
            example.and().andLike("modifiedUserId", "%" + attendGoOutRuleQuery.getModifiedUserId() + "%");
        }
        // 排序
        ExampleKit.setExampleOrder(example,attendGoOutRuleQuery.getOrders());
        return example;
    }
}




